#include  "stdio.h"
#include  "memory.h"
#include  "stdlib.h"
#include  "time.h"

#include "Carddrv.h"

#define __src
	#include  "Author.h"
#undef __src

char Author_FileName[30];

union _lic_data
{
	short int i[512];
	unsigned char ch[1024];
}LIC_DATA;

int FileExists(const char *filename)
{
	FILE *fp;
	if ((fp=fopen(filename,"r")) == NULL) return 0;
	fclose(fp);
	return 1;
}

int Create_Lic(unsigned char * Lic)
{
	FILE *fp;	
	int i;
	unsigned char ch[65];
	unsigned char key[]="D199902A";

	for (i=0;i<8;i++)
	{
		Des(&Lic[i*8],key,&ch[i*8]);
	}

	srand((unsigned)time(NULL));
	for (i=0;i<512;i++)
	{	  
		LIC_DATA.i[i] = rand();
	}
	for (i=0;i<sizeof(_sys_info);i++)
	{
		LIC_DATA.ch[i+0x308] = ch[i];
	}
	Set_Crc(&LIC_DATA.ch[0],1022);
	
	if ((fp=fopen(Author_FileName,"w+b")) == NULL) return (-1);
	i = fwrite(&LIC_DATA,sizeof(LIC_DATA),1,fp);
	fclose(fp);
	if (i != 1) return (-1);
	return 0;
}

int Get_Lic(unsigned char  * lic)
{
	FILE *fp;	
	int i;

	unsigned char key[]="D199902A";		

	if (FileExists(Author_FileName)) 
	{
		if ((fp=fopen(Author_FileName,"r+b")) == NULL) return (-1);
		i = fread(&LIC_DATA,1024,1,fp);
		fclose(fp);
		if (i != 1) return (-3); 
		if (Check_Crc(&LIC_DATA.ch[0],1022) !=0) return (-4);		
		for (i=0;i<8;i++)
		{
			UnDes(&LIC_DATA.ch[i*8+0x308],key,&lic[i*8]);
		}
		return (0);
	}
	return (-2);
}

